package com.ymt.bpm.service.engine.taskcenter.impl;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.dao.MDao;
import com.ymt.bpm.dao.Mapper;
import com.ymt.bpm.engine.EngineConst;
import com.ymt.bpm.model.PDraftTplRef;
import com.ymt.bpm.model.PPi;
import com.ymt.bpm.model.PTaskTodo;
import com.ymt.bpm.service.engine.bpd.BpdVersionCacheService;
import com.ymt.bpm.service.engine.taskcenter.TaskService;
import com.ymt.bpm.util.AES;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.Platform;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Johnny on 2017/4/17.
 */
@Service
public class TaskServiceImpl implements TaskService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MDao dao;

    @Autowired
    private BpdVersionCacheService bvcs;

    private String mapperQuery = "TaskQueryMapper";
    private String mapperTodo = "PTaskTodoMapper";
    private String mapperPi = "PPiMapper";

    @Override
    @Transactional(EngineConst.TRANSACTION_MANAGER)
    public JSONObject draftToTemplate(String tenantId, String loginName, String tiId, String title) {
        tiId = AES.decrypt(tiId);
        JSONObject rtn = new JSONObject();
        PTaskTodo todo = new PTaskTodo();
        if (tenantId!=null && StringUtils.isNumeric(tenantId)) todo.setTenantId(Integer.parseInt(tenantId));
        todo.setUserId(loginName);
        int count = dao.selectOne(mapperQuery, "countTemplate", todo);
        int limit = Platform.getInt("portal.process-tpl-limit");
        if (count>=limit) {
            rtn.put("limit", limit);
            return rtn;
        }
        todo.setTiId(tiId);
        todo = dao.selectOne(mapperTodo, "selectByObj", todo);
        todo.setPurpose(1);
        int rs = dao.update(mapperTodo, "updateByPrimaryKeySelective", todo);

        //update pi title
        PPi pi = new PPi();
        if (tenantId!=null && StringUtils.isNumeric(tenantId)) pi.setTenantId(Integer.parseInt(tenantId));
        pi.setPiEid(todo.getPiEid());
        pi = dao.selectOneByObject(Mapper.PPiMapper, "selectByObj", pi);
        pi.setTitle(title);
        rs += dao.update(Mapper.PPiMapper, "updateByPrimaryKeySelective", pi);

        rtn.put(Const.AJAX_RESULT, rs);
        return rtn;
    }

    @Override
    @Transactional(EngineConst.TRANSACTION_MANAGER)
    public JSONObject deleteDraft(String tenantId, String loginName, int purpose, String tiId, String piId) {
        tiId = AES.decrypt(tiId);
        PTaskTodo todo = new PTaskTodo();
        todo.setTiId(tiId);
        todo.setPiId(piId);
        todo.setUserId(loginName);
        todo.setStatus("00");
        todo.setPurpose(purpose);
        PPi pi = new PPi();
        pi.setPiId(piId);
        pi.setUserId(loginName);
        pi.setStatus("00");

        PDraftTplRef ref = new PDraftTplRef();
        ref.setTiId(tiId);

        if (tenantId!=null && StringUtils.isNumeric(tenantId)) {
            todo.setTenantId(Integer.parseInt(tenantId));
            pi.setTenantId(Integer.parseInt(tenantId));
            ref.setTenantId(Integer.parseInt(tenantId));
        }

        int rs = 0;
        rs += dao.delete(mapperTodo, "deleteByObj", todo);
        rs += dao.delete(mapperPi, "deleteByObj", pi);
        rs += dao.delete(Mapper.PDraftTplRefMapper, "deleteByPrimaryKey", ref);

        //TODO clean engine

        JSONObject rtn = new JSONObject();
        rtn.put(Const.AJAX_RESULT, rs);
        return rtn;
    }

    @Override
    @Transactional(EngineConst.TRANSACTION_MANAGER)
    public JSONObject cleanDraft(String tenantId, String loginName, int purpose) {
        PTaskTodo todo = new PTaskTodo();
        todo.setUserId(loginName);
        todo.setStatus("00");
        todo.setPurpose(purpose);
        PPi pi = new PPi();
        pi.setUserId(loginName);
        pi.setStatus("00");

        PDraftTplRef ref = new PDraftTplRef();

        if (tenantId!=null && StringUtils.isNumeric(tenantId)) {
            todo.setTenantId(Integer.parseInt(tenantId));
            pi.setTenantId(Integer.parseInt(tenantId));
            ref.setTenantId(Integer.parseInt(tenantId));
        }

        int rs = 0;
        rs += dao.delete(mapperTodo, "deleteByObj", todo);
        rs += dao.delete(mapperPi, "deleteByObj", pi);
        rs += dao.delete(Mapper.PDraftTplRefMapper, "cleanByDraft", ref);

        //TODO clean engine

        JSONObject rtn = new JSONObject();
        rtn.put(Const.AJAX_RESULT, rs);
        return rtn;
    }
}
